package com.example.mymarket.Fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.mymarket.Data_model.Channel;
import com.example.mymarket.Data_model.HomeData;
import com.example.mymarket.Data_model.NewGoodsList;
import com.example.mymarket.Data_model.ResponseData;
import com.example.mymarket.R;
import com.example.mymarket.activity.MainActivity;
import com.example.mymarket.activity.ProductDetailActivity;
import com.example.mymarket.adapter.ChannerlAdapter;
import com.example.mymarket.adapter.NewGoodsAdapter;
import com.example.mymarket.api.ApiService;
import com.example.mymarket.interfaces.ResponseCallback;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    // 用于传递参数的键名
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    // 从Activity传递过来的参数
    private String mParam1;
    private String mParam2;

    // 控件引用
    private com.youth.banner.Banner viewBanner;// 顶部轮播图
    private GridView gridView_channel,gridView_newGoodsList;// 频道网格视图, 新品列表网格视图
    private List<Channel> channels; // 频道数据列表
    private List<com.example.mymarket.Data_model.Banner> bannerList;// 轮播图数据列表

    public HomeFragment() {
        // Required empty public constructor
    }

    //绑定组件
    /**
     * 初始化视图控件
     * @param view Fragment的根视图
     */
    private void initView(View view) {
        viewBanner = view.findViewById(R.id.banner);
        gridView_channel = view.findViewById(R.id.gv_home_channel);
        gridView_newGoodsList = view.findViewById(R.id.gv_home_newGoodsList);
    }

    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        //初始化视图
        initView(view);

        // 调用API获取首页数据
        ApiService.GetHome(new ResponseCallback<ResponseData<HomeData>>() {
            @Override
            public void onSuccess(ResponseData<HomeData> result) {
                // 获取返回的轮播图和频道数据
                bannerList = result.getData().getBanner();
                channels = result.getData().getChannel();

                // 设置轮播图适配器和样式
                viewBanner.setAdapter(new BannerImageAdapter<com.example.mymarket.Data_model.Banner>(bannerList) {

                            @Override
                            public void onBindView(BannerImageHolder holder, com.example.mymarket.Data_model.Banner data, int position, int size) {
                                // 使用Glide加载图片并设置圆角
                                Glide.with(holder.imageView)
                                        .load(data.getUrl())
                                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                                        .into(holder.imageView);
                            }
                        }).addBannerLifecycleObserver(HomeFragment.this)
                        .setIndicator(new CircleIndicator(view.getContext()));
                // 设置频道网格适配器
                gridView_channel.setAdapter(new ChannerlAdapter(view.getContext(),channels));
                // 设置新品列表适配器
                NewGoodsAdapter newGoodsAdapter = new NewGoodsAdapter(requireContext(),result.getData().newGoodsList);
                gridView_newGoodsList.setAdapter(newGoodsAdapter);
                gridView_newGoodsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        NewGoodsList product = result.getData().newGoodsList.get(position);
                        Intent intent = new Intent(requireContext(), ProductDetailActivity.class);
                        intent.putExtra("productId", product.getId()); // 传递商品ID
                        requireContext().startActivity(intent);
                    }
                });
            }
        });




        return view;
    }
}